package com.ch.android.lookforkotlinv2.activity

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.ch.android.baselib.BaseBindingActivity
import com.ch.android.baselib.jiaN
import com.ch.android.baselib.schedule
import com.ch.android.lookforkotlinv2.R
import com.ch.android.lookforkotlinv2.databinding.ActivityMainBinding
import com.ch.android.lookforkotlinv2.vm.MainModel
import com.ch.android.networklib.RequestClient
import com.ch.android.networklib.data.BaseResultData
import com.ch.android.networklib.data.UserLoginData
import com.ch.android.networklib.data.UserLoginReq
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.schedulers.Schedulers
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


class MainActivity : BaseBindingActivity<ActivityMainBinding>() {
    private val mModel: MainModel by lazy {
        MainModel()
    }
    private val bannerData = mutableListOf<Int>()
    private val mCompositeDisposable: CompositeDisposable = CompositeDisposable()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mModel.title.value = "MainActivity"
        mBinding.model = mModel
        mBinding.activity = this
        initData()
        mBinding.youthBanner.setBannerGalleryEffect(80, 30)
        mBinding.youthBanner.isAutoLoop(false)
        mBinding.youthBanner.setAdapter(object : BannerImageAdapter<Int>(bannerData) {
            override fun onBindView(holder: BannerImageHolder, data: Int, position: Int, size: Int) {
                holder.imageView.maxWidth = 100
                holder.imageView.setImageResource(data);
            }
        })
        1.jiaN()
        var a: Int = 999.jiaN()
        Log.d("MainActivity", "a: $a")
    }


    private fun initData() {
        bannerData.add(R.drawable.cover1)
        bannerData.add(R.drawable.cover2)
        bannerData.add(R.drawable.cover3)
        bannerData.add(R.drawable.cover4)
    }

    override fun getLayout() = R.layout.activity_main

    fun onClickCoroutine() {
        lifecycleScope.launch(Dispatchers.IO) {
            val userData = getUserData()
            var xingBie = ""
            if (userData.result?.sex == 1L) {
                xingBie = "男"
            } else {
                xingBie = "女"
            }
            withContext(Dispatchers.Main) {
                Toast.makeText(this@MainActivity, "性别：${xingBie}", Toast.LENGTH_SHORT).show()
//                Toast.makeText(this@MainActivity, userData.message, Toast.LENGTH_SHORT).show()
            }
        }
    }



    // 使用协程完成用户登录
    private suspend fun getUserData(): BaseResultData<UserLoginData?> {
        val userReq = UserLoginReq("ch1", "123456")
        return RequestClient.getApiService(this).userLogin(userReq)
    }

    fun onClick(view: View) {
        val userReq = UserLoginReq("ch1", "123456")
        // 使用RxJava完成用户登录

        val disposableA = RequestClient.getApiService(this).userLoginA(userReq)
            .schedule()
            .subscribe({
                mModel.title.value = it.message
                Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
            }, {
                mModel.title.value = it.message ?: "error"
            })

        val disposable = RequestClient.getApiService(this).userLoginA(userReq)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                mModel.title.value = it.message
                Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()

            }, {
                mModel.title.value = it.message ?: "error"
            })

        mCompositeDisposable.add(disposable)
        /*
                val disposable = MyHttpClient.getApiService().userLoginA(userReq).schedule()
                    .subscribe({
                        mModel.title.value = it.message
                        Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
                    }, {
                        mModel.title.value = it.message ?: "error"
                    })
        */
    }
    fun openUserLogin(view: View) {
        startActivity(Intent(this, UserLoginActivity::class.java))
//        startActivity(Intent(this, DemoActivity2::class.java))
    }

    override fun onDestroy() {
        super.onDestroy()
        mCompositeDisposable.clear()
    }
}